package cn.weekdragon.gscore.adapter.model;

import cn.hutool.core.util.StrUtil;
import cn.weekdragon.gscore.adapter.bot.Bot;
import cn.weekdragon.gscore.adapter.model.enums.EventEnum;
import cn.weekdragon.gscore.adapter.model.message.Message;
import cn.weekdragon.gscore.adapter.model.message.MessageChain;
import cn.weekdragon.gscore.adapter.model.message.Plain;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Data
@Slf4j
public class EventExt {

    public static final ThreadLocal<EventExt> EVENT_EXT = new ThreadLocal<>();


    /**
     * 消息类型
     */
    private String type;
    /**
     * 群号
     */
    private String groupId;
    /**
     * 发送者名称
     */
    private String senderName;

    /**
     * 连接对象的ID，一般是机器人ID
     */
    private String connectId;

    /**
     * 消息ID
     */
    private String messageId;

    /**
     * 消息序号 主要用于 red协议的转发消息
     */
    private String msgSeq;
    /**
     * 消息来源 in
     */
    private MessageChain inMessage;
    /**
     * 消息返回 out
     */
    private MessageChain outMessage;
    /**
     * 当前消息来源的bot
     */
    private Bot bot;
    /**
     * 用户标识
     */
    private String userId;
    /**
     * 用户类型
     */
    private String userType;

    private Map<String,Object> attributeMap = new HashMap<>();



    /**
     * argsText 缓存用的字段，不建议直接使用
     */
    private transient String tempText;




    public static void clear() {
        EVENT_EXT.remove();
    }

    public static EventExt get() {
        return EVENT_EXT.get();
    }

    public static void setCurrent(EventExt ext) {
        EVENT_EXT.set(ext);
    }

    public static SendResp autoMessage(String text) {
        return autoMessage(get(),text);
    }

    public static SendResp autoMessage(Message message) {
        return autoMessage(get(),message);
    }
    public static SendResp autoMessage(EventExt ext,String text) {
        return autoMessage(ext,new Plain(text));
    }

    public static SendResp autoMessage(EventExt ext, Message message) {
        if(Objects.isNull(ext)){
            log.warn("ext为空");
            return null;
        }
        Bot bot = ext.getBot();
        SendResp resp = new SendResp(bot);
        if(Objects.isNull(bot)){
            log.error("发送消息失败，adapter为空");
            return resp;
        }
        EventExt getExt = EventExt.get();
        try {
            EventExt.setCurrent(ext);
            String type = ext.getType();
            if (EventEnum.GROUP_MESSAGE.getType().equals(type)) {
                return bot.sendGroupMessage(ext.getGroupId(), message);
            } else if (EventEnum.PRIVATE_MESSAGE.getType().equals(type)) {
                return bot.sendPrivateMessage(ext.getGroupId(), message);
            } else {
                log.warn("未知的消息回复");
                return resp;
            }
        }finally {
            if(Objects.isNull(getExt)){
                EventExt.clear();
            }
        }
    }

    public static void sendDebugMessage(Message message) {
    }
    public static void sendDebugMessage(String message) {
        sendDebugMessage(new Plain(message));
    }

    public String argsText() {
        if(StrUtil.isNotEmpty(tempText)){
            return tempText;
        }
        return inMessage.contentToString();
    }

    public Long getUserIdAsLong(){
        return Long.parseLong(userId);
    }

    public void setAttribute(String key,Object data){
        attributeMap.put(key,data);
    }

    public Object getAttribute(String key){
        return attributeMap.get(key);
    }

}
